package com.kcht.np.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kcht.np.common.exception.ParameterException;
import com.kcht.np.entity.SysDictData;
import com.kcht.np.entity.SysDictType;
import com.kcht.np.service.SysDictDataService;
import com.kcht.np.service.SysDictTypeService;
import com.kcht.np.util.JsonResult;
import com.kcht.np.vo.dict.DictDataCanCreateInfo;
import com.kcht.np.vo.dict.DictDataCanModifyInfo;
import com.kcht.np.vo.dict.DictTypeCanCreateInfo;
import com.kcht.np.vo.dict.DictTypeCanModifyInfo;
import com.kcht.np.vo.page.ConditionsQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "[ 系统管理 ] 字典管理")
@SuppressWarnings("all")
@RestController
@RequestMapping("/sys/dict")
public class SysDictController {
    @Autowired
    private SysDictTypeService dictTypeService;
    @Autowired
    private SysDictDataService dictDataService;

    /*--------------字典查询----------------------*/
    @ApiOperation(value = "字典类型查询所有", notes = "字典类型查询所有")
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('pre_dict')")
    public JsonResult<List<SysDictType>> queryDictTypes() {
        return JsonResult.success(dictTypeService.list());
    }

    @ApiOperation(value = "字典类型分页查询", notes = "字典类型分页查询所有")
    @PostMapping("/all")
    @PreAuthorize("hasAnyAuthority('pre_dict')")
    public JsonResult<List<SysDictType>> queryDictTypes(@RequestBody ConditionsQuery parameter, BindingResult br) {
        try{
            IPage<SysDictType> iPage = dictTypeService.page(parameter);
            return JsonResult.success(iPage.getPages(), iPage.getTotal(), iPage.getRecords());
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }
    @ApiOperation(value = "字典类型通过id查询", notes = "字典类型通过id查询")
    @GetMapping("/by/{id}")
    @PreAuthorize("hasAnyAuthority('pre_dict')")
    public JsonResult<List<SysDictType>> queryDictTypeById(@PathVariable("id") Long id) {
        try{
            return JsonResult.success(dictTypeService.getById(id));
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }
    @ApiOperation(value = "字典数据分页查询", notes = "字典数据分页查询所有")
    @PostMapping("/data")
    @PreAuthorize("hasAnyAuthority('pre_dict')")
    public JsonResult<List<SysDictData>> queryDictDatas(@RequestBody ConditionsQuery parameter, BindingResult br) {
        try {
            IPage<SysDictData> iPage = dictDataService.page(parameter);
            return JsonResult.success(iPage.getPages(), iPage.getTotal(), iPage.getRecords());
        } catch (ParameterException e) {
            return JsonResult.result(400, e.getMessage());
        }
    }

    @ApiOperation(value = "字典数据通过id查询", notes = "字典数据通过id查询")
    @GetMapping("/data/{id}")
    @PreAuthorize("hasAnyAuthority('pre_dict')")
    public JsonResult<SysDictData> queryDictDataById(@PathVariable Long id) {
        try {
            return JsonResult.success(dictDataService.getById(id));
        } catch (ParameterException e) {
            return JsonResult.result(400, e.getMessage());
        }
    }

    @ApiOperation(value = "字典数据查询", notes = "字典数据查询")
    @GetMapping("/{dictType}")
    @PreAuthorize("hasAnyAuthority('pre_dict')")
    public JsonResult<List<SysDictData>> queryDictDataByDictType(@PathVariable String dictType) {
        return JsonResult.success(dictDataService.queryDictDataByDictType(dictType));
    }

    /*--------------字典新增----------------------*/
    @ApiOperation(value = "新增字典类型", notes = "新增字典类型")
    @PostMapping("/create")
    @PreAuthorize("hasAnyAuthority('pre_dict:create')")
    public JsonResult<SysDictType> createDictType(@RequestBody @Validated DictTypeCanCreateInfo parameter, BindingResult br) {
        try{
            dictTypeService.save(parameter);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

    @ApiOperation(value = "新增字典数据", notes = "新增字典数据")
    @PostMapping("/data/create")
    @PreAuthorize("hasAnyAuthority('pre_dict:create')")
    public JsonResult<SysDictData> createDictData(@RequestBody @Validated DictDataCanCreateInfo parameter, BindingResult br) {
        try{
            dictDataService.save(parameter);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }
    /*--------------字典更新----------------------*/
    @ApiOperation(value = "更新字典类型", notes = "更新字典类型")
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('pre_dict:update')")
    public JsonResult<SysDictType> updateDictType(@RequestBody @Validated DictTypeCanModifyInfo parameter, BindingResult br) {
        try{
            dictTypeService.update(parameter);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

    @ApiOperation(value = "更新字典数据", notes = "更新字典数据")
    @PutMapping("/data/update")
    @PreAuthorize("hasAnyAuthority('pre_dict:update')")
    public JsonResult<SysDictData> updateDictData(@RequestBody @Validated DictDataCanModifyInfo parameter, BindingResult br) {
        try{
            dictDataService.update(parameter);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

    /*--------------字典删除----------------------*/
    @ApiOperation(value = "删除字典类型", notes = "删除字典类型")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('pre_dict:delete')")
    public JsonResult<SysDictType> deleteDictType(@PathVariable Long id) {
        try{
            dictTypeService.delete(id);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }
    @ApiOperation(value = "删除字典数据", notes = "删除字典数据")
    @DeleteMapping("/data/{id}")
    @PreAuthorize("hasAnyAuthority('pre_dict:delete')")
    public JsonResult<SysDictData> deleteDictData(@PathVariable Long id) {
        try{
            dictDataService.delete(id);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }
}
